package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询某个时间段内的运营数据
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime beginTime, LocalDateTime endTime) {
        // 构造参数
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("begin", beginTime);
        paramMap.put("end", endTime);
        // 所有订单数
        Integer totalOrderCount = ordersMapper.getOrderCountByTime(paramMap);
        totalOrderCount = totalOrderCount == null ? 0 : totalOrderCount;
        // 新增用户数
        Integer newUser = userMapper.getNewUserByTime(paramMap);
        // 营业额
        paramMap.put("status", Orders.COMPLETED);
        Double turnover = ordersMapper.getTurnoverByTime(paramMap);
        turnover = turnover == null ? 0.0 : turnover;
        // 有效订单数
        Integer validOrderCount = ordersMapper.getOrderCountByTime(paramMap);
        validOrderCount = validOrderCount == null ? 0 : validOrderCount;
        // 计算订单完成率
        Double orderCompletionRate = 0.0;
        if (totalOrderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
        }
        // 计算平均客单价(营业额/有效订单数)
        Double unitPrice = 0.0;
        if (validOrderCount != 0) {
            // 计算unitPrice并保留两位小数
            unitPrice = Math.round((turnover.doubleValue() / validOrderCount) * 100.0) / 100.0;
        }

        // 构造参数
        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUser)
                .build();
        return businessDataVO;
    }

    /**
     * 查询订单管理数据
     * @param beginTime
     * @return
     */
    public OrderOverViewVO getOverviewOrders(LocalDateTime beginTime) {
        // 构造参数
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("begin", beginTime);
        // 待接单数量
        paramMap.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = ordersMapper.getCountByMap(paramMap);
        // 待派送数量
        paramMap.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = ordersMapper.getCountByMap(paramMap);
        // 已完成数量
        paramMap.put("status", Orders.COMPLETED);
        Integer completedOrders = ordersMapper.getCountByMap(paramMap);
        // 已取消数量
        paramMap.put("status", Orders.CANCELLED);
        Integer cancelledOrders = ordersMapper.getCountByMap(paramMap);
        // 全部订单
        paramMap.put("status", null);
        Integer allOrders = ordersMapper.getCountByMap(paramMap);

        // 构造参数
        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
        return orderOverViewVO;
    }

    /**
     * 查询菜品总览
     * @return
     */
    public DishOverViewVO getOverviewDishes(){
        // 构造参数
        HashMap<String, Object> paramMap = new HashMap<>();
        // 已启售数量
        paramMap.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.getCountByMap(paramMap);
        // 已停售数量
        paramMap.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.getCountByMap(paramMap);
        // 构造参数
        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return dishOverViewVO;
    }

    /**
     * 查询套餐总览
     * @return
     */
    public SetmealOverViewVO getOverviewSetmeals(){
        // 构造参数
        HashMap<String, Object> paramMap = new HashMap<>();
        // 已启售数量
        paramMap.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.getCountByMap(paramMap);
        // 已停售数量
        paramMap.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.getCountByMap(paramMap);
        // 构造参数
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return setmealOverViewVO;
    }
}
